﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.Material;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine;
using PloobsEngine.Modelo;
using PloobsEngine.SceneControl;
using PloobsEngine.Cameras;
using Microsoft.Xna.Framework;

namespace Etapa1.Baker
{
    public class BakerShader
    {
        public BakerShader()
        {
            effect = EngineStuff.CustomContentManager.GetAsset<Effect>("effects/bakerphong");
        }

        public void Draw(IObject obj,IModelo modelo, ICamera camera)
        {
            //effect.Parameters[""].SetValue(1);
            //effect.Parameters[""].SetValue(50);
            effect.Parameters["Texture"].SetValue(modelo.getTexture(TextureType.DIFFUSE));
            effect.Parameters["VP"].SetValue(camera.View * camera.Projection);

            Matrix[] boneTransforms = modelo.getBonesTransformation();           

            for (int i = 0; i < modelo.MeshNumber; i++)
            {
                BatchInformation[] bi = modelo.GetBatchInformation(i);
                Matrix w1 = boneTransforms[modelo.GetParentBoneIndex(i)];
                effect.Parameters["World"].SetValue(w1);
                for (int j = 0; j < bi.Count(); j++)
                {

                    effect.Begin();
                    EngineStuff.GraphicsDevice.VertexDeclaration = bi[j].VertexDeclaration;
                    EngineStuff.GraphicsDevice.Vertices[0].SetSource(modelo.GetVertexBuffer(i), bi[j].StreamOffset, bi[j].VertexStride);
                    EngineStuff.GraphicsDevice.Indices = modelo.GetIndexBuffer(i);

                    effect.CurrentTechnique.Passes[0].Begin();
                    EngineStuff.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, bi[j].BaseVertex, 0, bi[j].NumVertices, bi[j].StartIndex, bi[j].PrimitiveCount);
                    effect.CurrentTechnique.Passes[0].End();
                    effect.End();
                }

            }

        }

        Effect effect;
    }
}
